package com.ny.service.impl;

import com.ny.entity.User;
import com.ny.mapper.UserMapper;
import com.ny.service.EmailService;
import com.ny.service.UserService;

import com.ny.until.PasswordUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public int addUser(User user) {
        // 把数据交给数据库之前先进行加密：MD5

          String  pwd = PasswordUtil.encryptPassword(user.getPassword());// 进行加密  pwd = gfalk;hgalkhg;
          user.setPassword(pwd);// 把加密的密码交给user
          return userMapper.addUser(user);
    }

    @Override
    public User findUser(String username, String password) {
        // 先尝试用加密后的密码查询
        String pwd = PasswordUtil.encryptPassword(password);
        User user = userMapper.findUser(username, pwd);
        
        // 如果没找到，再尝试用原始密码查询（兼容数据库中明文存储的密码）
        if (user == null) {
            user = userMapper.findUser(username, password);
        }
        
        return user;
    }

    @Override
    public User findUserByEmail(String email) {
        return userMapper.findUserByEmail(email);
    }

    @Override
    public int updatePassword(String password, String email) {
        // 密码加密
        String pwd = PasswordUtil.encryptPassword(password);
        return userMapper.updatePassword(pwd,email);
    }
    
    @Override
    public List<User> findAllUsers() {
        return userMapper.findAllUsers();
    }

    @Override
    public User findUserById(Integer userId) {
        return userMapper.findUserById(userId);
    }

    @Override
    public int deleteUserById(Integer userId) {
        return userMapper.deleteUserById(userId);
    }

    @Override
    public boolean updatePasswordWithOld(Integer userId, String oldPassword, String newPassword) {
        // 先查询用户信息
        User user = userMapper.findUserById(userId);
        
        if (user == null) {
            return false;
        }
        
        // 验证旧密码是否正确
        String encryptedOldPassword = PasswordUtil.encryptPassword(oldPassword);
        if (!user.getPassword().equals(encryptedOldPassword)) {
            return false;
        }
        
        // 更新为新密码
        String encryptedNewPassword = PasswordUtil.encryptPassword(newPassword);
        userMapper.updatePasswordById(encryptedNewPassword, userId);
        return true;
    }

    @Autowired
    private EmailService emailService;
    // 给发件人发送验证码
@Override
public void sendToEmail(String toEmail) {
    // 执行发送验证码的逻辑
    String code = emailService.getCode();
    emailService.sendEmail(toEmail, code);
    // 把验证码存储到Redis中
    redisTemplate.opsForValue().set(toEmail, code);
}


    // 验证码校验
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Override
    public boolean checkCode(String toEmail, String code) {
        // 获取Redis中存储的验证码
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String redisCode = ops.get(toEmail);
        // 判断验证码是否一致，同时避免空指针异常
        return redisCode != null && redisCode.equals(code);
    }


}